<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\admin\model\ThemeModel;
use app\portal\model\DiaryModel;
use cmf\controller\AdminBaseController;

/**
 * Class AdminDiaryController 日记本管理控制器
 * @package app\portal\controller
 */
class AdminDiaryController extends AdminBaseController
{
    /**
     * 文章标签管理
     * @adminMenu(
     *     'name'   => '日记本',
     *     'parent' => 'portal/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '日记本',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        /*$content = hook_one('portal_admin_diary_index_view');

        if (!empty($content)) {
            return $content;
        }*/

        $diaryModel = new DiaryModel();
        $diaries = $diaryModel->where(function ($query) {
            $keyword = $this->request->param('keyword');
            if (!empty($keyword)) {
                $query->where('msg', 'like', "$keyword%");
            }
        })
            ->order('create_time desc')
            ->paginate();

        $this->assign("arrStatus", DiaryModel::$STATUS);
        $this->assign("diaries", $diaries);
        $this->assign('page', $diaries->render());
        return $this->fetch();
    }

    /**
     * 添加日记
     * @adminMenu(
     *     'name'   => '添加日记',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加日记',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $diaryModel = new DiaryModel();
        $this->assign("arrStatus", $diaryModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 添加日记提交
     * @adminMenu(
     *     'name'   => '添加日记提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加日记提交',
     *     'param'  => ''
     * )
     */
    public function addPost()
    {

        $tagData = $this->request->param();
        $result  = $this->validate(
            $tagData,
            [
                //'name' => 'require|max:20',
                'msg' => 'require',
            ],
            [
                'msg.require' => '日记内容必填！',
                //'name.max'     => '标签名称超过最大长度！！',
            ]
        );
        $tagData['year'] = date('Y');
        $tagData['create_time'] = time();

        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $diaryModel = new DiaryModel();
        $diaryModel->save($tagData);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 更新日记状态
     * @adminMenu(
     *     'name'   => '更新日记状态',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '更新日记状态',
     *     'param'  => ''
     * )
     */
    public function upStatus()
    {
        $intId     = $this->request->param("id");
        $intStatus = $this->request->param("status");
        $intStatus = $intStatus ? 1 : 0;
        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        $diaryModel = new DiaryModel();
        $diaryModel->where("id", $intId)->update(["status" => $intStatus]);

        $this->success(lang("SAVE_SUCCESS"));

    }

    /**
     * 删除日记
     * @adminMenu(
     *     'name'   => '删除日记',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除日记',
     *     'param'  => ''
     * )
     */
    public function delete()
    {
        $intId = $this->request->param("id", 0, 'intval');

        if (empty($intId)) {
            $this->error(lang("NO_ID"));
        }

        DiaryModel::where('id', $intId)->delete();
        //PortalTagPostModel::where('tag_id', $intId)->delete();
        $this->success(lang("DELETE_SUCCESS"));
    }

    /**
     * 编辑日记
     * @adminMenu(
     *     'name'   => '编辑日记',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑日记',
     *     'param'  => ''
     * )
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $content = hook_one('portal_admin_diary_edit_view');

        if (!empty($content)) {
            return $content;
        }

        $id = $this->request->param('id', 0, 'intval');

        $diaryModel = new DiaryModel();
        $post            = $diaryModel->where('id', $id)->find();
        $this->assign('post', $post);
        $this->assign("arrStatus", $diaryModel::$STATUS);
        return $this->fetch();
    }

    /**
     * 编辑日记提交
     * @adminMenu(
     *     'name'   => '编辑日记提交',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑日记提交',
     *     'param'  => ''
     * )
     * @throws \think\Exception
     */
    public function editPost()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            //$post   = $data['post'];

            $diaryModel = new DiaryModel();
            $diaryModel->where('id', $post['id'])->update(["msg" => $post['msg'], 'create_time'=>$post['create_time'], 'year'=>$post['year']]);

            $this->success('保存成功!');
        }
    }
}
